package com.lishe.lishe.lishe.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.LinkedList;

/**
 * Created by shino on 2017/9/19.
 */

public class ImageViewPagerAdapter extends PagerAdapter {
    LinkedList<ImageView> imageViews;

    public ImageViewPagerAdapter(LinkedList<ImageView> imageViews) {
        this.imageViews=imageViews;
    }

    @Override
    public int getCount() {
        return imageViews==null?0:imageViews.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ViewGroup view= (ViewGroup) imageViews.get(position).getParent();
        if(view !=null){
            view.removeAllViews();
        }
        container.addView(imageViews.get(position));
        return imageViews.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
        container.removeView(imageViews.get(position));
    }
}
